package com.gold.util;


import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by ChangYY <502341194@163.com>
 * 2015-03-19
 * 17:32
 * TO DO What
 */
public class MsgUtils{

//    private static String serviceURL = PropertyUtil.getPropertiesByName("msgServerUrl","sys");
//
//    private static String sn = PropertyUtil.getPropertiesByName("sn", "sys");
//
//    private static String pwd = PropertyUtil.getPropertiesByName("pwd", "sys");

    /*
	 * 方法名称：getMD5 功 能：字符串MD5加密 参 数：待转换字符串 返 回 值：加密之后字符串
	 */
    public static String getMD5(String sourceStr) throws UnsupportedEncodingException {
        String resultStr = "";
        try {
            byte[] temp = sourceStr.getBytes();
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(temp);
            // resultStr = new String(md5.digest());
            byte[] b = md5.digest();
            for (int i = 0; i < b.length; i++) {
                char[] digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                        '9', 'A', 'B', 'C', 'D', 'E', 'F' };
                char[] ob = new char[2];
                ob[0] = digit[(b[i] >>> 4) & 0X0F];
                ob[1] = digit[b[i] & 0X0F];
                resultStr += new String(ob);
            }
            return resultStr;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

//    /*
//     * 方法名称：mt 功 能：发送短信 参 数：mobile,content,ext,stime,rrid(手机号，内容，扩展码，定时时间，唯一标识)
//     * 返 回 值：唯一标识，如果不填写rrid将返回系统生成的
//     */
//    public static String mt(String mobile, String content, String ext, String stime,
//                     String rrid) throws UnsupportedEncodingException{
//        String result = "";
//        String soapAction = "http://tempuri.org/mt";
//        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
//        xml += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
//        xml += "<soap:Body>";
//        xml += "<mt xmlns=\"http://tempuri.org/\">";
//        xml += "<sn>" + sn + "</sn>";
//        xml += "<pwd>" + getMD5(sn+pwd) + "</pwd>";
//        xml += "<mobile>" + mobile + "</mobile>";
//        xml += "<content>" +  content + "</content>";
//        xml += "<ext>" + ext + "</ext>";
//        xml += "<stime>" + stime + "</stime>";
//        xml += "<rrid>" + rrid + "</rrid>";
//        xml += "</mt>";
//        xml += "</soap:Body>";
//        xml += "</soap:Envelope>";
//        System.out.println(""+xml);
//        URL url;
//        try {
//            url = new URL(serviceURL);
//
//            URLConnection connection = url.openConnection();
//            HttpURLConnection httpconn = (HttpURLConnection) connection;
//            ByteArrayOutputStream bout = new ByteArrayOutputStream();
//            bout.write(xml.getBytes("GBK"));
//            byte[] b = bout.toByteArray();
//            httpconn.setRequestProperty("Content-Length", String
//                    .valueOf(b.length));
//            httpconn.setRequestProperty("Content-Type",
//                    "text/xml; charset=gb2312");
//            httpconn.setRequestProperty("SOAPAction", soapAction);
//            httpconn.setRequestMethod("POST");
//            httpconn.setDoInput(true);
//            httpconn.setDoOutput(true);
//
//            OutputStream out = httpconn.getOutputStream();
//            out.write(b);
//            out.close();
//
//            InputStreamReader isr = new InputStreamReader(httpconn
//                    .getInputStream());
//            BufferedReader in = new BufferedReader(isr);
//            String inputLine;
//            while (null != (inputLine = in.readLine())) {
//                Pattern pattern = Pattern.compile("<mtResult>(.*)</mtResult>");
//                Matcher matcher = pattern.matcher(inputLine);
//                while (matcher.find()) {
//                    result = matcher.group(1);
//                }
//            }
//            return result;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "";
//        }
//    }

    /**
     * 获取六位随机数
     * @return
     */
    public static String getSixRandom(){
        StringBuffer stringBuffer = new StringBuffer("");
        for(int i=0;i<6;i++){
            int randomCode = new Random().nextInt(10);
            stringBuffer.append(randomCode);
        }
        return stringBuffer.toString();
    }





}
